package com.sinoeyes.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * 统一API响应结果封装
 * R
 *
 * @author aries.hao
 */
@Data
@ApiModel(description = "返回信息")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "状态码", required = true)
    private String code;
    @ApiModelProperty(value = "是否成功", required = true)
    private boolean success;
    @ApiModelProperty(value = "承载数据")
    private T data;
    @ApiModelProperty(value = "返回消息", required = true)
    private String msg;

    private Result(RCode rCode) {
        this(rCode, null);
    }

    private Result(RCode rCode, T data) {
        this(rCode, data, true);
    }

    private Result(RCode rCode, T data, boolean success) {
        this(rCode.getCode(), data, rCode.getMessage(), success);
    }

    private Result(String code, T data, String msg, boolean success) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = success;
    }

    /**
     * 成功反悔
     * @param data 数据对象
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data){
        return new Result(RCode.SUCCESS, data);
    }

    /**
     * 成功反悔
     * @param rCode 状态码
     * @param data 数据对象
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(RCode rCode, T data){
        return new Result(rCode, data);
    }

    /**
     * 失败反悔状态码
     * @param rCode 状态码
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(RCode rCode) {
        return failure(rCode, null);
    }

    /**
     * 失败返回状态码及数据
     * @param rCode 状态码
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(RCode rCode, T data) {
        return new Result(rCode, data, false);
    }

    /**
     * 失败返回状态码及数据
     * @param code 状态码
     * @param message 数据
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(String code, String message) {
        return new Result(code, null, message, false);
    }
}

